package com.pingbu.tijian.hospital.backapi.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pingbu.common.bean.ResultBean;
import com.pingbu.common.utils.PbFileUtils;
import com.pingbu.common.utils.PbSecretUtils;
import com.pingbu.tijian.hospital.backapi.bean.entity.StudentGroup;
import com.pingbu.tijian.hospital.backapi.bean.entity.StudentZuoye;
import com.pingbu.tijian.hospital.backapi.bean.entity.UserPermission;
import com.pingbu.tijian.hospital.backapi.bean.entity.VideoUpload;
import com.pingbu.tijian.hospital.backapi.config.GlobalSet;
import com.pingbu.tijian.hospital.backapi.mapper.StudentMapper;
import com.pingbu.tijian.hospital.backapi.mapper.UserInfoMapper;
import com.pingbu.tijian.hospital.backapi.mapper.UserPermissionMapper;
import com.pingbu.tijian.hospital.backapi.mapper.VideoUploadMapper;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class VideoUploadService {

    private static Logger logger = LoggerFactory.getLogger(VideoUploadService.class);

    @Autowired
    private GlobalSet globalSet;
    @Autowired
    private VideoUploadMapper videoUploadMapper;

    /**
     * 得到所有的视频
     * @param param
     * @return
     */
    public ResultBean<List<VideoUpload>> listVideo(VideoUpload param){

        List<VideoUpload> lst1 = videoUploadMapper.list(param);
        return ResultBean.create(0,"success",lst1);
    }

    /**
     * 读取某个视频的路径
     * @param param
     * @return
     */
    public ResultBean<String> readLen(VideoUpload param){
        param.setView_path(globalSet.getZuoyu_upload_url_root() + "video");
        return ResultBean.create(0,"success",param.getView_path());
    }

    /**
     * 删除某个视频
     * @param param
     * @return
     */
    public ResultBean<List<VideoUpload>> videoDelete(VideoUpload param){
        String fullPath = globalSet.getZuoye_upload_root_path() + "/video/" + param.getVideo_path();
        File file = new File(fullPath);
        file.delete();
        videoUploadMapper.delete(param);
        return ResultBean.create(0,"success");
    }

    /**
     * 上传视频，指定上传格式为MP4，并保存到指定文件夹下
     * @param uploadFile1
     * @return
     * @throws IOException
     */
    public ResultBean<Integer> videoPath(MultipartFile uploadFile1) throws IOException {

        String fileName = uploadFile1.getOriginalFilename();
        int pointLastIndex = fileName.lastIndexOf(".");
        String pointmp4 = fileName.substring(pointLastIndex);
        if (pointmp4.equals(".mp4")) {
            InputStream inputStream = uploadFile1.getInputStream();
            String filePath = globalSet.getZuoye_upload_root_path() + "/video";
            //C://zuoye_upload//video
            Map<String, String> map = PbFileUtils.getFilePath(filePath, uploadFile1);
            PbFileUtils.saveFile(map.get("fullPath"),inputStream);

            String video_md5 = DigestUtils.md5DigestAsHex(new FileInputStream(map.get("fullPath")));

            VideoUpload videoUpload = new VideoUpload();

            List<VideoUpload> videoList = videoUploadMapper.list(videoUpload);

            for(VideoUpload videoUpload1 : videoList){
                if(videoUpload1.getVideo_md5() != null && videoUpload1.getVideo_md5().equals(video_md5)){
                    return  ResultBean.create(10,"err");
                }
            }

            videoUpload.setNoid(PbSecretUtils.uuid());
            videoUpload.setVideo_name(fileName);
            videoUpload.setVideo_path(map.get("realPath"));
            videoUpload.setVideo_md5(video_md5);
            videoUploadMapper.insert(videoUpload);
            return  ResultBean.create(0,"success");
        }
        return  ResultBean.create(1,"err");
    }


}
